# 获取用户角色
from typing import List

from fastapi import Depends, APIRouter
from pydantic import BaseModel

from api.acl.user import GetUser_roles, Add_user, Delete_user,GetRoles,Add_user_roles
from util.JWT import get_current_user

acl_user = APIRouter()

# 获取所有角色
@acl_user.get("/roles")
async def roles(id: dict = Depends(get_current_user)):
    df = GetRoles()
    if df.empty:
        return {
            "code": 204,
            "status": "no",
            "msg": "没有内容",
            "data": {}
        }
    df.fillna('', inplace=True)
    # 将DataFrame转换为字典列表
    dict_list = df.to_dict(orient='records')
    return {
        "code": 200,
        "status": "ok",
        "msg": "success",
        "data": dict_list
    }

# 获取用户角色
@acl_user.get("/user_roles")
async def user_roles(page: int = 1, page_size: int = 5, id: dict = Depends(get_current_user)):
    df = GetUser_roles()
    if df.empty:
        return {
            "code": 204,
            "status": "no",
            "msg": "没有内容",
            "total": 0,
            "data": {}
        }
    df.fillna('', inplace=True)
    total = len(df)
    offset = (page - 1) * page_size
    df = df.iloc[offset: offset + page_size]
    # 将DataFrame转换为字典列表
    dict_list = df.to_dict(orient='records')
    # 将字典列表转换为指定格式的JSON字符串
    return {
        "code": 200,
        "status": "ok",
        "msg": "success",
        "total": total,
        "data": dict_list
    }


class AddUserType(BaseModel):
    username: str
    fullname: str
    pwd: str


# 添加用户
@acl_user.post("/add_user")
async def add_user(data: AddUserType, id: dict = Depends(get_current_user)):
    i = Add_user(data.username, data.fullname, data.pwd)
    if i == 1:
        return {
            "code": 200,
            "status": "ok",
            "msg": "添加成功",
            "data": {}
        }
    else:
        return {
            "code": 204,
            "status": "no",
            "msg": "添加失败",
            "data": {}
        }


@acl_user.delete("/delete_user")
async def delete_user(users_id: int, id: dict = Depends(get_current_user)):
    if Delete_user(users_id):
        return {
            "code": 200,
            "status": "ok",
            "msg": "删除成功",
            "data": {}
        }
    else:
        return {
            "code": 204,
            "status": "no",
            "msg": "删除失败",
            "data": {}
        }


class AddUserRolesType(BaseModel):
    users_id: int
    full_name: str
    roles: List[int]
@acl_user.post("/add_user_roles")
async def add_user_roles(data: AddUserRolesType, id: dict = Depends(get_current_user)):
    sqlList=[]
    sqlList.append(("DELETE FROM user_roles WHERE users_id = ?", [data.users_id]))
    for v in data.roles:
        sqlList.append(("INSERT INTO user_roles(users_id, role_id) VALUES(?, ?)", [data.users_id, v]))
    if Add_user_roles(sqlList):
        return {
            "code": 200,
            "status": "ok",
            "msg": "添加成功",
            "data": {}
        }
    else:
        return {
            "code": 204,
            "status": "no",
            "msg": "添加失败",
            "data": {}
        }

